package infomation 
{
	import org.airmvc.Controller;
	import org.airmvc.ModulesManager;
	import utils.T;
	import vo.CastleVO;
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class InfomationC extends Controller 
	{
		
		public function InfomationC() 
		{
			super();
		}
		
		override protected function addListeners():void 
		{
			interest(Nws.INFOMATION_SWITCH_CITY, handleCityInfo);
			interest(Nws.INFOMATION_UPDATE_CITY, updateCastle);
			interest(Nws.INFOMATION_UPDATE_ACTION, updateAction);
			interest(Nws.INFOMATION_UPDATE_TIME, function(type:int):void { send(UPDATE_TIME,type); } );
			interest(Nws.INFOMATION_UPDATE_ORDER,function(type:int):void { send(UPDATE_ORDER,type); } );
			interest(Nws.MENU_SHOW_OR_HIDE, function(isShow:Boolean):void { send(SHOW_OR_HIDE, isShow); } );
		}
		
		private function updateAction(date:int,gold:int = 0,rice:int = 0,step:int = 0):void 
		{
			send(UPDATE_ACTION, date, gold, rice, step);
		}
		
		private function updateCastle(id:int):void 
		{
			var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, id);
			if (c != curCastle)
			{
				//计算应该在左侧还是右侧显示>256左
				var xml:XML = T.db.getXml(Cnt.XML_CASTLE).castle.(@id == String(id))[0];
				var tx:int;
				var ty:int = 49;
				if (int(xml.@x) > 256)
					tx = 32;
				else
					tx = 256;
				//计算士兵数量
				var n:int;
				for each(var i:int in c.genList)
				{
					var gen:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, i) as GeneralVO;
					n += gen.soliders;
				}
				
				send(UPDATE_CITY_INFO,c,tx,ty,n);
			}
		}
		
		private function handleCityInfo(type:int):void 
		{
			send(OPEN_CITY_INFO,type);
		}
		
		override protected function startup(... args):void 
		{
			//初始化视图
			if (view == null)
			{
				view = new InfomationV();
				registerView(view);
			}
		}
		
		private var curCastle:CastleVO;
		
		/**打开或关闭视图*/
		static public const SHOW_OR_HIDE:String = "show_or_hide";
		/**打开城镇信息面板，type:1~4*/
		static public const OPEN_CITY_INFO:String = "open_city_info";
		/**更新城镇信息面板*/
		static public const UPDATE_CITY_INFO:String = "update_city_info";
		/**更新年月信息*/
		static public const UPDATE_TIME:String = "update_time";
		/**更新军令数*/
		static public const UPDATE_ORDER:String = "update_order";
		/**更新机动等信息，date(==0隐藏),gold,rice,step*/
		static public const UPDATE_ACTION:String = "update_action";
	}
	
}